package com.webdev.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.webdev.domain.Personnel;
import com.webdev.service.PersonnelService;
import com.webdev.service.imp.PersonnelServiceImp;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LogInServlet")
public class LogInServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * 登录请求，如果登录成功返回员工信息否则返回错误信息
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		PersonnelService service = new PersonnelServiceImp();
		
		response.setCharacterEncoding("utf-8");
		JSONObject jo = new JSONObject();
		if(service.logIn(username, password)) {			
			Personnel personnel = new Personnel();
			personnel = service.getPersonnelByUsername(username);
			jo = service.getJSONObject(personnel);
			jo.put("state", "true");
		}
		else {						
			jo.put("state", "fasle");
		}
		response.getWriter().write(jo.toString());
	}
}
